import Vue from 'vue'
import VueRouter from 'vue-router'
import DengLu from 'C:/Users/34352/Desktop/vue01/vue_shop01/src/components/DengLu.vue'
import HomeVue from 'C:/Users/34352/Desktop/vue01/vue_shop01/src/components/HomeVue.vue'
import WelcomeVue from '@/components/WelcomeVue.vue'
import UsersVue from '@/components/user/UsersVue.vue'
import RightsVue from '@/components/power/RightsVue.vue'
import RolesVue from '@/components/power/RolesVue.vue'
import CateVue from '@/components/goods/CateVue.vue'
import ParamsVue from '@/components/goods/ParamsVue.vue'
import ListVue from '@/components/goods/ListVue.vue'
import AddVue from '@/components/goods/AddVue.vue'
import OrderVue from '@/components/order/OrderVue.vue'
import ReportVue from '@/components/report/ReportVue.vue'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: DengLu },
    { path: '/home', component: HomeVue ,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: WelcomeVue },
        { path: '/users', component: UsersVue },
        { path: '/rights', component: RightsVue },
        { path: '/roles', component: RolesVue },
        { path: '/categories', component: CateVue },
        { path: '/params', component: ParamsVue },
        { path: '/goods', component: ListVue },
        { path: '/goods/add', component: AddVue },
        { path: '/orders', component: OrderVue },
        { path: '/reports', component: ReportVue }
      ]
    }
  
  ]
})

// 挂载路由导航守卫
// to表示将要访问的路径，from表示从哪个路径跳转而来
// next()放行,next('/login)表示强制跳转
router.beforeEach((to, from, next) => {
  if(to.path === '/login') return next();
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
    next()
})



export default router
